package com.LatitudeYao;

/**
 * Created by GTS on 2017/8/17.
 * o(n)计算斐波那契数列
 */
public class FabYM {
    public static long  fabCalculation(int n){
        long pre1 = 0;
        long pre2 = 1;
        if(n<0){
            return -1;
        }
        else if (n == 0){
            return pre1;
        }
        else if (n == 1){
            return pre2;
        }
        long result = 0;
        for (int i = 2; i <=n ; i++) {
            result = pre1 + pre2;//计算结果
            pre1 = pre2;//缓存前第2个数
            pre2 = result;//缓存前第 1个数
        }
        System.out.println(result);
        return result;
    }

    public static void main(String[] args) {
        fabCalculation(10);
    }
}
